package com.resou.reader.tinder.favorites;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.request.RequestOptions;
import com.resou.reader.R;
import com.resou.reader.data.tinder.tinder.CartBook;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class FavoritesAdapter extends RecyclerView.Adapter<FavoritesAdapter.collectionHolder> {

    private List<CartBook> mDataList;
    private List<CartBook> mSelectedList;
    private static final String TAG = "FavoritesAdapter";
    //    SparseArray<CartBook> mDataList = new SparseArray<>();
    private boolean isSelectedAll;
    private OnItemSelectedListener mItemSelectedListener;
    private FavoritesActivity mActivity;

    /**
     * 删除已选列表
     */
    public void deleteSelectedItems() {
        for (int i = 0; i < mSelectedList.size(); i++) {
            CartBook selected = mSelectedList.get(i);
            int selectedIndex = mDataList.indexOf(selected);
            mDataList.remove(selected);
            mItemSelectedListener.onItemDeleted(selected.getId());
            notifyItemRemoved(selectedIndex);
        }
        mSelectedList.clear();
    }

    public FavoritesAdapter(FavoritesActivity activity) {
        mDataList = new ArrayList<>();
        mSelectedList = new ArrayList<>();
        mActivity = activity;
        mItemSelectedListener = activity;
    }

    @NonNull
    @Override
    public collectionHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_collection, parent, false);
        return new collectionHolder(view, mItemSelectedListener, mSelectedList);
    }

    @Override
    public void onBindViewHolder(@NonNull final collectionHolder holder, final int position) {
        if (mActivity.isInEditMode) {
            holder.ivChooseStatus.setVisibility(View.VISIBLE);
            if (mSelectedList.contains(mDataList.get(position))) {
                holder.ivChooseStatus.setSelected(true);
            } else {
                holder.ivChooseStatus.setSelected(false);
            }
        }else{
            holder.ivChooseStatus.setVisibility(View.GONE);
        }
        CartBook book = mDataList.get(position);
        holder.bind(mActivity, book);
    }

    public int getSelectedItemCount() {
        return mSelectedList.size();
    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    public void setSelectedAll(boolean selectedAll) {
        isSelectedAll = selectedAll;
        if (selectedAll) {
            for (CartBook s : mDataList) {
                if (mSelectedList.contains(s)) {
                    continue;
                }
                mSelectedList.add(s);
            }
        } else {
            inverseSelection();
        }
        notifyDataSetChanged();
    }

    /**
     * 反选
     */
    public void inverseSelection() {
        for (CartBook s : mDataList) {
            if (mSelectedList.contains(s)) {
                mSelectedList.remove(s);
            } else {
                mSelectedList.add(s);
            }
        }
    }

    public boolean isSelectedAll() {
        return isSelectedAll;
    }

    public void clearSelected() {
        mSelectedList.clear();
    }

    public void setData(List<CartBook> books) {
        mDataList = books;
        notifyDataSetChanged();
    }

    static class collectionHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_book_image)
        ImageView tvBookImage;
        @BindView(R.id.tv_book_name)
        TextView tvBookName;
        @BindView(R.id.iv_personnel)
        ImageView ivPersonnel;
        @BindView(R.id.tv_book_author)
        TextView tvBookAuthor;
        @BindView(R.id.tv_book_introduction)
        TextView tvBookIntroduction;
        @BindView(R.id.iv_choose_status)
        ImageView ivChooseStatus;
        @BindView(R.id.fl_choose_status_container)
        FrameLayout flChooseStatusContainer;
        @BindView(R.id.rl_item_favorites)
        RelativeLayout rlItemFavorites;
        private OnItemSelectedListener mListener;
        private List<CartBook> mList;

        public collectionHolder(@NonNull View itemView, OnItemSelectedListener itemSelectedListener, List<CartBook> list) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            mListener = itemSelectedListener;
            mList = list;
        }

        void bind(final FavoritesActivity activity, final CartBook book) {
            //书名
            tvBookName.setText(book.getNovelName());
            RequestOptions options = new RequestOptions();
            options.format(DecodeFormat.PREFER_RGB_565);
            //封面
            Glide.with(tvBookImage).load(book.getCoverUrl()).apply(options).into(tvBookImage);
            //作者名
            tvBookAuthor.setText(book.getAuthorName());
            //简介
            tvBookIntroduction.setText(book.getNovelDescription());

            rlItemFavorites.setOnClickListener(v -> {
                if (activity.isInEditMode) {
                    if (ivChooseStatus.isSelected()) {
                        //已选择，点击后从已选列表中删除
                        ivChooseStatus.setSelected(false);
                        mList.remove(book);
                        mListener.onItemSelected();
                    } else {
                        //未选择，点击后添加到已选列表
                        ivChooseStatus.setSelected(true);
                        mList.add(book);
                        mListener.onItemSelected();
                    }
                } else {
                    mListener.onItemClicked(book);
                }
            });
        }
    }

    interface OnItemSelectedListener {
        void onItemSelected();

        void onItemDeleted(String bookId);

        void onItemClicked(CartBook book);
    }
}
